package com.example.myapplication;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class AddItemActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText3;
    private EditText editText4;
    private EditText editText5;
    private EditText editText6;
    private EditText editText7;
    private EditText editText8;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_item);
        initView();


    }

    private void initView() {
        editText3 = (EditText) findViewById(R.id.editText3);
        editText4 = (EditText) findViewById(R.id.editText4);
        editText5 = (EditText) findViewById(R.id.editText5);
        editText6 = (EditText) findViewById(R.id.editText6);
        editText7 = (EditText) findViewById(R.id.editText7);
        editText8 = (EditText) findViewById(R.id.editText8);
        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
             //1.获取数据
                String name=editText3.getText().toString();
                String desc=editText4.getText().toString();
                String remark=editText5.getText().toString();
                String price=editText6.getText().toString();
                String kindid=editText7.getText().toString();
                String avail=editText8.getText().toString();
                //2.打包数据
                Map<String,String> map=new HashMap<>();
                map.put("itemName",name);
                map.put("itemDesc",desc);
                map.put("itemRemark",remark);
                map.put("initPrice",price);
                map.put("kindId",kindid);
                map.put("avail",avail);
                //3.发送数据
                try {
                    String result= OkHttpUtil.postRequest("http://172.18.85.254:8080/auction/api/items",map);
                    Toast.makeText(this,result,Toast.LENGTH_LONG).show();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void submit() {
        // validate
        String editText3String = editText3.getText().toString().trim();
        if (TextUtils.isEmpty(editText3String)) {
            Toast.makeText(this, "editText3String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText4String = editText4.getText().toString().trim();
        if (TextUtils.isEmpty(editText4String)) {
            Toast.makeText(this, "editText4String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText5String = editText5.getText().toString().trim();
        if (TextUtils.isEmpty(editText5String)) {
            Toast.makeText(this, "editText5String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText6String = editText6.getText().toString().trim();
        if (TextUtils.isEmpty(editText6String)) {
            Toast.makeText(this, "editText6String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText7String = editText7.getText().toString().trim();
        if (TextUtils.isEmpty(editText7String)) {
            Toast.makeText(this, "editText7String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText8String = editText8.getText().toString().trim();
        if (TextUtils.isEmpty(editText8String)) {
            Toast.makeText(this, "editText8String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }
}
